package org.metasketch.simulator.perspective;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;

import org.metasketch.editor.Editor;
import org.metasketch.editor.Project;
import org.metasketch.editor.perspectives.ILayout;
import org.metasketch.editor.perspectives.PerspectiveManager;
import org.metasketch.editor.perspectives.SimplePerspectiveManager;
import org.metasketch.editor.timeline.SnapshotManager;

public class MiniPerspectiveChooser extends Group implements
		EventHandler<MouseEvent> {

	// private Editor editor;
	BorderPane pane;
	private ToggleGroup group;
	private ILayout currentLayout;
	Map<String, ILayout> storedLayouts = new HashMap<String, ILayout>();
	private SimplePerspectiveManager manager;
	private boolean created;
	private Project project;

	// private String currentPerspectiveName;

	public void clear() {

		storedLayouts.clear();
	}

	public MiniPerspectiveChooser(SimplePerspectiveManager manager) {

		// this.pane = pane;
		this.manager = manager;

		// setup();
		// create();

	}

	public void setPane(BorderPane pane) {

		this.pane = pane;
		// pane.getChildren()
		// pane.setTop(this);

		create();
		// TextField textField = new TextField();
		// textField.setMinHeight(20);
		// textField.setMinWidth(120);
		//
		// HBox hb = new HBox();
		// hb.getChildren().addAll();
		// box.getChildren().addAll(hb);
	}

	protected void create() {

		if (created)
			return;

		created = true;

		HBox box = new HBox();
		getChildren().add(box);

		// String defaultPerspectiveName = SnapshotManager
		// .getCurrentPerspectiveName();

		// if (defaultPerspectiveName == null)
		String defaultPerspectiveName = manager.getDefaultPerspective();

		group = new ToggleGroup();
		List<String> names = manager.getNames();
		for (String name : names) {
			ToggleButton button = new ToggleButton(name);

			button.setMaxWidth(120);
			button.setMinWidth(120);
			button.setPrefWidth(120);

			button.setToggleGroup(group);
			box.getChildren().add(button);
			button.setOnMouseClicked(this);

			if (defaultPerspectiveName.equals(name)) {
				button.setSelected(true);
				setPerspective(name);
			}

		}
	}

	@Override
	public void handle(MouseEvent arg0) {
		ToggleButton toggle = (ToggleButton) arg0.getSource();
		String name = toggle.getText();
		setPerspective(name);
	}

	private void setPerspective(String name) {

		if (storedLayouts.containsKey(name)) {
			currentLayout = storedLayouts.get(name);
		} else {
			currentLayout = manager.getLayout(name);
			// TODO fix this to use something else in order to access the
			// simulation information which is detached from the project data.
			// currentLayout.setup(editor.getProject());
		}

		// pane.setCenter(null);
		// pane.getChildren().clear();

		// SnapshotManager.setCurrentPerspectiveName(name);

		currentLayout.apply(pane);
		currentLayout.setup(project);

		if (!storedLayouts.containsKey(name))
			storedLayouts.put(name, currentLayout);
	}

	public void update() {
		if (currentLayout != null)
			currentLayout.apply(pane);
	}

	public void setup(Project project) {
		this.project = project;

		if (currentLayout != null) {
			currentLayout.apply(pane);
			currentLayout.setup(project);
		}
	}

	// public void setProject(Project project) {
	// // TODO Auto-generated method stub
	// setup(pane);
	// }

}
